<template>
    <zh-form ref="formRef" v-loading="data.loading" :model="data.formData" label-width="160px" :span="24" :rules="data.rules">
      <zh-form-select  :disabled="props.disabled||(props.labelType!=null&&props.labelType!='')" prop="labelType" label="标签类型" placeholder="请输入标签类型" v-model="data.formData.labelType" :options="data.dictData.labelType"></zh-form-select>
      <zh-form-input
          prop="title"
          label="标签名称"
          :disabled="props.disabled"
          v-model="data.formData.title"
      ></zh-form-input>
      <zh-form-select prop="status" :disabled="props.disabled" label="状态" placeholder="请选择" :options="data.statusDict" v-model="data.formData.status"></zh-form-select>
      <zh-form-input
          prop="remarks"
          label="描述"
          type="textarea"
          :disabled="props.disabled"
          v-model="data.formData.remarks"
      ></zh-form-input>
    </zh-form>
</template>

<script lang="ts" setup name="dictLabelForm">
import { ElMessage } from 'element-plus';

//------------------定义子组件---------------------------
const formRef = ref();
//------------------接收传入参数(组件)-------------------
const props = defineProps({
  labelCode: {
    type: String,
    default: '',
  },
  labelType: {
    type: String,
    default: '',
  },
  disabled: {
    type: Boolean,
    default: true,
  },
  btnLoding: Boolean,
});
const emits = defineEmits(['update:btnLoding', 'canner', 'refreshTable']);
//------------------初始化------------------------------
onMounted(() => {
  getDict();
  getDetail();
});
//------------------内部变量定义-------------------------
const data = reactive({
  formData: {labelType:props.labelType,status:'1'} as any,
  statusDict: [
		{
			dictKey: '0',
			dictValue: '无效',
		},
		{
			dictKey: '1',
			dictValue: '有效',
		},
	],
  detailVisible: true,
  loading: false,
  rules: {
  },
  dictData: {} as any,
  areaCodeDict: [] as any,
  campusParam: {} as any,
});

// 查询数据字典信息
const getDict = () => {
  const params = {} as any;
  params.dictFields = 'labelType';
  $api.common.getBatchDict.post(params).then((res) => {
    data.dictData = res.result;
  });
};

//获取详情
const getDetail = () => {
  if (!props.labelCode) {
    return;
  }
  data.loading = true;
  $api.common.send
  .post('dictLabel0002', { labelCode: props.labelCode })
  .then((res: any) => {
    data.formData = res.result || {};
    data.loading = false;
    data.campusParam.schoolId = data.formData.schoolId;
  })
  .catch(() => {
    data.loading = false;
  });
};

//保存
async function save() {
  let checkFlag = await formRef.value.checkForm();
  if (checkFlag) {
    emits('update:btnLoding', true);
    if (data.formData.labelCode) {
      $api.common.send
      .post('dictLabel0005', data.formData)
      .then(() => {
        ElMessage.success('修改成功');
        emits('update:btnLoding', false);
        emits('canner', (data.detailVisible = false));
        emits('refreshTable');
      })
      .catch(() => {
        emits('update:btnLoding', false);
        emits('canner', (data.detailVisible = true));
      });
    } else {
      $api.common.send
      .post('dictLabel0004', data.formData)
      .then(() => {
        ElMessage.success('新增成功');
        emits('update:btnLoding', false);
        emits('canner', (data.detailVisible = false));
        emits('refreshTable');
      })
      .catch(() => {
        emits('update:btnLoding', false);
        emits('canner', (data.detailVisible = true));
      });
    }
  }
}
watch(
	() => props.labelType,
	(val: any) => {
		data.formData.labelType=val
	},
	{
		deep: true,
	}
);
//------------设置给父组件调用的数据/方法(组件)-----------
defineExpose({ save });
</script>
<style lang="scss" scoped></style>
